﻿// AmplitudeModulationControl.xaml.cs (c) Copyright 2009, Mike Hodnick : www.hodnick.com

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Kindohm.KSynth.Library;
using System.Windows.Input;

namespace Kindohm.KSynth.AudioCanvas
{
    public partial class AmplitudeModulationControl : UserControl, ICanvasControl
    {
        Oscillator oscillator;
        Color oldColor;
        bool envelopeIsVisible;

        public AmplitudeModulationControl()
        {
            InitializeComponent();
            this.Loaded += new System.Windows.RoutedEventHandler(AmplitudeModulationControl_Loaded);
        }

        void AmplitudeModulationControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            this.MouseLeave += new System.Windows.Input.MouseEventHandler(AmplitudeModulationControl_MouseLeave);
            this.MouseEnter += new System.Windows.Input.MouseEventHandler(AmplitudeModulationControl_MouseEnter);
        }

        void AmplitudeModulationControl_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.HideMenu();
        }

        void AmplitudeModulationControl_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.ShowMenu();
        }

        private void Path_MouseEnter(object sender, MouseEventArgs e)
        {
            Path path = sender as Path;
            SolidColorBrush brush = path.Fill as SolidColorBrush;
            this.oldColor = brush.Color;
            brush.Color = Color.FromArgb(255, 66, 66, 66);
        }

        private void Path_MouseLeave(object sender, MouseEventArgs e)
        {
            Path path = sender as Path;
            SolidColorBrush brush = path.Fill as SolidColorBrush;
            brush.Color = oldColor;
        }

        private void Path_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.envelopeIsVisible)
            {
                this.envelopeIsVisible = false;
                if (this.HideEnvelopeButtonClicked != null)
                    this.HideEnvelopeButtonClicked(this, EventArgs.Empty);
            }
            else
            {
                this.envelopeIsVisible = true;
                if (this.ShowEnvelopeButtonClicked != null)
                    this.ShowEnvelopeButtonClicked(this, EventArgs.Empty);
            }
        }

        public bool EnvelopeIsVisible
        {
            get { return this.envelopeIsVisible; }
            set { this.envelopeIsVisible = value; }
        }

        void ShowMenu()
        {
            this.menuCanvas.Visibility = Visibility.Visible;
        }

        void HideMenu()
        {
            this.menuCanvas.Visibility = Visibility.Collapsed;
        }

        public TranslateTransform Translation
        {
            get { return this.translation; }
        }

        public Oscillator Oscillator
        {
            get { return this.oscillator; }
            set
            {
                this.oscillator = value;
                this.oscillator.AmplitudeModulate(this.frequencyUpDown, NumericUpDown.ValueProperty);
            }
        }

        public event EventHandler ShowEnvelopeButtonClicked;
        public event EventHandler HideEnvelopeButtonClicked;

    }
}
